//
//  CZDiscoverViewController.m
//  小和微博
//
//  Created by 董晓合 on 15/2/15.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "CZDiscoverViewController.h"
#import "CZSearchBar.h"

@interface CZDiscoverViewController () <UITextFieldDelegate>
/**
 *  搜索框
 */
@property (nonatomic, weak) CZSearchBar *searchTf;
@end

@implementation CZDiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
//    添加搜索框
    /*
    UISearchBar *searchBar = [[UISearchBar alloc]init];
    self.navigationItem.titleView = searchBar;
    */
//    自定义搜索框
  //  UITextField *searchTf = [[UITextField alloc] init];
    CZSearchBar *searchTf = [[CZSearchBar alloc] init];
    searchTf.frame = CGRectMake(0, 0, 300, 30);
    self.navigationItem.titleView = searchTf;
    searchTf.delegate = self;
    self.searchTf = searchTf;
    

}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    // 1.添加按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancle)];
    
//    2.替换leftView
    self.searchTf.leftIconName = @"settings_statistic_triangle";

}
- (void)cancle
{
    // 0.清除取消按钮
    self.navigationItem.rightBarButtonItem = nil;
    // 1.关闭键盘
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    // 2.还原leftView
    self.searchTf.leftIconName = @"searchbar_searchlist_search_icon";
}

#pragma mark - 监听scorllerView的拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 关闭键盘
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

@end
